$(function () {
    // 是否在查询中标志
    var flag = false;
    // 记录搜索框的值
    var search_value = '';

    //总页面数
    function totalPage() {
        var total = 0;
        // ajax同步请求
        $.ajax({
            type: "get",
            url: "userlist?action=totalPage",
            async: false,
            success: function (date) {
                total = date;
            }
        });
        return total;
    }
    var totalpage = totalPage();

    // 页面加载
    var nowpage = 1;
    function showPage(nowpage) {
        $.ajax({
            type: "get",
            url: "userlist?action=changePage&page=" + nowpage,
            dataType: "json",
            async: false,
            success: function (date) {
                add(date);
            }
        });
        function add(data) {
            // tr事件与input事件不冲突标志
            var openipt = true;
            var trArraay = $('table tbody').children();
            for (var i = 0; i < trArraay.length; i++) {
                trArraay[i].remove();
            }
            for (var i = 0; i < data.length; i++) {
                var id = data[i].id;
                var index = i + 1 + (nowpage - 1) * 10;
                var name = data[i].name;
                var sex = data[i].sex == 0 ? '男' : '女';
                var age = data[i].age;
                var number = data[i].number;
                var username = data[i].username;
                var email = data[i].email;
                var state = data[i].state == 0 ? '激活' : '冻结';
                var newNode = $("<tr><td>" + index + "</td><td>" + name + "</td><td>" + sex + "</td><td>" + age + "</td><td>" + number + "</td><td>" + username + "</td><td>" + email + "</td><td><input type='button' class='change' id='" + id + "' value='修改'><input type='button' class='delete' id='" + id + "' value='" + state + "'></tr></td>");
                $('table').append(newNode);
            }

            // 遍历表格中的所有input
            // 修改按钮
            $('table tbody .change').each(function () {
                $(this).click(function (event) {
                    // 阻止子元素事件冒泡到父元素
                    event.stopPropagation();
                    var ids = $(this).attr("id");//获取它的id属性值
                    $('.user_list .hidden').css('display', 'block');
                    $('.user_list .hidden .change').css('display', 'block');
                    $('.user_list .hidden .change .determine').click(function () {
                        $.ajax({
                            type: "get",
                            url: "userlist?action=resetPwd&id=" + ids,
                            success: function (date) {
                                if (date == 1) {
                                    $('.user_list .hidden .change i').html('重置成功！！！');
                                    // 定时1秒关闭
                                    setTimeout(function () {
                                        $('.user_list .hidden .change i').html('');
                                        $('.user_list .hidden').css('display', 'none');
                                        $('.user_list .hidden .change').css('display', 'none');
                                    }, 1000);
                                } else {
                                    $('.user_list .hidden .change i').html('重置失败！！！');
                                }
                            }
                        });
                    });

                    $('.user_list .hidden .change .cancel').click(function () {
                        $('.user_list .hidden .change i').html('');
                        $('.user_list .hidden').css('display', 'none');
                        $('.user_list .hidden .change').css('display', 'none');
                    });
                });
            });
            // 删除按钮
            $('table tbody .delete').each(function () {
                $(this).click(function (event) {
                    // 阻止子元素事件冒泡到父元素
                    event.stopPropagation();
                    // 获取登录用户名
                    function getCookie(name) {
                        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
                        if (arr = document.cookie.match(reg)) {
                            return unescape(arr[2]);
                        } else {
                            return null;
                        }
                    }

                    var $thisipt = $(this);
                    var ids = $thisipt.attr("id");//获取它的id属性值
                    $('.user_list .hidden').css('display', 'block');
                    $('.user_list .hidden .delete').css('display', 'block');
                    var state;
                    $.ajax({
                        type: "get",
                        url: "userlist?action=getState&id=" + ids,
                        async: false,
                        success: function (date) {
                            state = date;
                        }
                    });
                    if (state == 1) {
                        $('.user_list .hidden .delete span').html('是否冻结用户！！！');
                    }
                    if (state == 0) {
                        $('.user_list .hidden .delete span').html('是否激活用户！！！');
                    }
                    $('.user_list .hidden .delete .determine').click(function () {
                        $.ajax({
                            type: "get",
                            url: "userlist?action=delete&id=" + ids + "&state=" + state + "&admin_username=" + getCookie("user_name"),
                            success: function (date) {
                                if (date == 1) {
                                    if (state == 1) {
                                        $('.user_list .hidden .delete i').html('冻结成功！！！');
                                    }
                                    if (state == 0) {
                                        $('.user_list .hidden .delete i').html('激活成功！！！');
                                    }
                                    setTimeout(function () {
                                        openipt = true;
                                        $('.user_list .hidden .delete i').html('');
                                        $('.user_list .hidden').css('display', 'none');
                                        $('.user_list .hidden .delete').css('display', 'none');
                                        // 刷新页面
                                        $('table tbody').html('');
                                        showPage(nowpage);
                                        $('.page').html('');
                                        showButton();
                                    }, 1000);
                                } else {
                                    if (state == 1) {
                                        $('.user_list .hidden .delete i').html('冻结失败！！！');
                                    }
                                    if (state == 0) {
                                        $('.user_list .hidden .delete i').html('激活失败！！！');
                                    }
                                }
                            }
                        });
                    });
                    $('.user_list .hidden .delete .cancel').click(function () {
                        $('.user_list .hidden .delete i').html('');
                        $('.user_list .hidden').css('display', 'none');
                        $('.user_list .hidden .delete').css('display', 'none');
                    });

                });
            });

            // 遍历tbody中的tr
            $('table tbody tr').each(function () {
                $(this).click(function () {
                    var ids = $(this).children().children('input').attr('id');//获取它的input的id属性值
                    $('.user_list .hidden').css('display', 'block');
                    $('.user_list .hidden .detail').css('display', 'block');
                    $.ajax({
                        type: "get",
                        url: "userlist?action=detail&id=" + ids,
                        dataType: "json",
                        success: function (date) {
                            var name = date.name;
                            var sex = date.sex == 0 ? '男' : '女';
                            var age = date.age;
                            var number = date.number;
                            var username = date.username;
                            var email = date.email;
                            var adddate = date.adddate;
                            var deletedate = date.state == 0 ? date.deletedate : '';
                            var state = date.state == 0 ? '已冻结' : '已激活';
                            var changeuser = date.state == 0 ? date.changeuser : '';
                            var newNode = $("<li><span class='key'>姓名：</span><span class='value'>" + name + "</span></li><li><span class='key'>性别：</span><span class='value'>" + sex + "</span></li><li><span class='key'>年龄：</span><span class='value'>" + age + "</span></li><li><span class='key'>授权id：</span><span class='value'>" + number + "</span></li><li><span class='key'>用户名：</span><span class='value'>" + username + "</span></li><li><span class='key'>邮箱：</span><span class='value'>" + email + "</span></li><li><span class='key'>注册时间：</span><span class='value'>" + adddate + "</span></li><li><span class='key'>最后冻结时间：</span><span class='value'>" + deletedate + "</span></li><li><span class='key'>状态：</span><span class='value'>" + state + "</span></li><li><span class='key'>执行冻结人：</span><span class='value'>" + changeuser + "</span></li>");
                            $('.user_list .hidden .detail ul').append(newNode);
                        }
                    });
                });
                $('.user_list .hidden .detail i').click(function () {
                    $('.user_list .hidden').css('display', 'none');
                    $('.user_list .hidden .detail').css('display', 'none');
                    $('.user_list .hidden .detail ul').html('');
                });
            })
        }
    }
    showPage(nowpage);

    // 查找页面加载
    function searchPage(nowpage, nowusername, nameOrnum) {
        $.ajax({
            type: "get",
            url: "userlist?action=searchPage&page=" + nowpage + "&value=" + nowusername + "&nameOrnum=" + nameOrnum,
            dataType: "json",
            success: function (date) {
                add(date);
            }
        });
        function add(data) {
            // tr事件与input事件不冲突标志
            var openipt = true;
            var trArraay = $('table tbody').children();
            for (var i = 0; i < trArraay.length; i++) {
                trArraay[i].remove();
            }
            for (var i = 0; i < data.length; i++) {
                var id = data[i].id;
                var index = i + 1 + (nowpage - 1) * 10;
                var name = data[i].name;
                var sex = data[i].sex == 0 ? '男' : '女';
                var age = data[i].age;
                var number = data[i].number;
                var username = data[i].username;
                var email = data[i].email;
                var state = data[i].state == 0 ? '激活' : '冻结';
                var newNode = $("<tr><td>" + index + "</td><td>" + name + "</td><td>" + sex + "</td><td>" + age + "</td><td>" + number + "</td><td>" + username + "</td><td>" + email + "</td><td><input type='button' class='change' id='" + id + "' value='修改'><input type='button' class='delete' id='" + id + "' value='" + state + "'></tr></td>");
                $('table').append(newNode);
            }

            // 遍历表格中的所有input
            // 修改按钮
            $('table tbody .change').each(function () {
                $(this).click(function (event) {
                    // 阻止子元素事件冒泡到父元素
                    event.stopPropagation();
                    var ids = $(this).attr("id");//获取它的id属性值
                    $('.user_list .hidden').css('display', 'block');
                    $('.user_list .hidden .change').css('display', 'block');
                    $('.user_list .hidden .change .determine').click(function () {
                        $.ajax({
                            type: "get",
                            url: "userlist?action=resetPwd&id=" + ids,
                            success: function (date) {
                                if (date == 1) {
                                    $('.user_list .hidden .change i').html('重置成功！！！');
                                    // 定时1秒关闭
                                    setTimeout(function () {
                                        $('.user_list .hidden .change i').html('');
                                        $('.user_list .hidden').css('display', 'none');
                                        $('.user_list .hidden .change').css('display', 'none');
                                    }, 1000);
                                } else {
                                    $('.user_list .hidden .change i').html('重置失败！！！');
                                }
                            }
                        });
                    });

                    $('.user_list .hidden .change .cancel').click(function () {
                        $('.user_list .hidden .change i').html('');
                        $('.user_list .hidden').css('display', 'none');
                        $('.user_list .hidden .change').css('display', 'none');
                    });
                });
            });
            // 删除按钮
            $('table tbody .delete').each(function () {
                $(this).click(function (event) {
                    // 阻止子元素事件冒泡到父元素
                    event.stopPropagation();
                    // 获取登录对象用户名
                    function getCookie(name) {
                        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
                        if (arr = document.cookie.match(reg)) {
                            return unescape(arr[2]);
                        } else {
                            return null;
                        }
                    }

                    var $thisipt = $(this);
                    var ids = $thisipt.attr("id");//获取它的id属性值
                    $('.user_list .hidden').css('display', 'block');
                    $('.user_list .hidden .delete').css('display', 'block');
                    var state;
                    $.ajax({
                        type: "get",
                        url: "userlist?action=getState&id=" + ids,
                        async: false,
                        success: function (date) {
                            state = date;
                        }
                    });
                    if (state == 1) {
                        $('.user_list .hidden .delete span').html('是否冻结用户！！！');
                    }
                    if (state == 0) {
                        $('.user_list .hidden .delete span').html('是否激活用户！！！');
                    }
                    $('.user_list .hidden .delete .determine').click(function () {
                        $.ajax({
                            type: "get",
                            url: "userlist?action=delete&id=" + ids + "&state=" + state + "&admin_username=" + getCookie("user_name"),
                            success: function (date) {
                                if (date == 1) {
                                    if (state == 1) {
                                        $('.user_list .hidden .delete i').html('冻结成功！！！');
                                    }
                                    if (state == 0) {
                                        $('.user_list .hidden .delete i').html('激活成功！！！');
                                    }
                                    setTimeout(function () {
                                        openipt = true;
                                        $('.user_list .hidden .delete i').html('');
                                        $('.user_list .hidden').css('display', 'none');
                                        $('.user_list .hidden .delete').css('display', 'none');
                                        // 刷新页面
                                        $('table tbody').html('');
                                        searchPage(nowpage, search_value, nameOrnum);
                                        $('.page').html('');
                                        showButton();
                                    }, 1000);
                                } else {
                                    if (state == '1') {
                                        $('.user_list .hidden .delete i').html('冻结失败！！！');
                                    }
                                    if (state == '0') {
                                        $('.user_list .hidden .delete i').html('激活失败！！！');
                                    }
                                }
                            }
                        });
                    });
                    $('.user_list .hidden .delete .cancel').click(function () {
                        $('.user_list .hidden .delete i').html('');
                        $('.user_list .hidden').css('display', 'none');
                        $('.user_list .hidden .delete').css('display', 'none');
                    });

                });
            });

            // 遍历tbody中的tr
            $('table tbody tr').each(function () {
                $(this).click(function () {
                    var ids = $(this).children().children('input').attr('id');//获取它的input的id属性值
                    $('.user_list .hidden').css('display', 'block');
                    $('.user_list .hidden .detail').css('display', 'block');
                    $.ajax({
                        type: "get",
                        url: "userlist?action=detail&id=" + ids,
                        dataType: "json",
                        success: function (date) {
                            var name = date.name;
                            var sex = date.sex == 0 ? '男' : '女';
                            var age = date.age;
                            var passid = date.passid;
                            var username = date.username;
                            var email = date.email;
                            var adddate = date.adddate;
                            var deletedate = date.state == 0 ? date.deletedate : '';
                            var state = date.state == 0 ? '已冻结' : '已激活';
                            var bindingdate = date.bindingdate;
                            var newNode = $("<li><span class='key'>姓名：</span><span class='value'>" + name + "</span></li><li><span class='key'>性别：</span><span class='value'>" + sex + "</span></li><li><span class='key'>年龄：</span><span class='value'>" + age + "</span></li><li><span class='key'>授权id：</span><span class='value'>" + passid + "</span></li><li><span class='key'>用户名：</span><span class='value'>" + username + "</span></li><li><span class='key'>邮箱：</span><span class='value'>" + email + "</span></li><li><span class='key'>授权时间：</span><span class='value'>" + adddate + "</span></li><li><span class='key'>最后冻结时间：</span><span class='value'>" + deletedate + "</span></li><li><span class='key'>状态：</span><span class='value'>" + state + "</span></li><li><span class='key'>添加时间：</span><span class='value'>" + bindingdate + "</span></li>");
                            $('.user_list .hidden .detail ul').append(newNode);
                        }
                    });
                });
                $('.user_list .hidden .detail i').click(function () {
                    $('.user_list .hidden').css('display', 'none');
                    $('.user_list .hidden .detail').css('display', 'none');
                    $('.user_list .hidden .detail ul').html('');
                });
            })
        }
    }

    // 按钮加载
    function showButton() {
        var append_html = '<input type="button" value="上一页" class="prev_page">';
        for (var i = 0; i < totalpage - 1; i++) {
            if (totalpage > 8 && nowpage > 5 && i < nowpage - 3) {
                if (i < 2) {
                    append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
                }
                else if (i == 2) {
                    append_html += '<span class="page_dot">•••</span>';
                }
            } else if (totalpage > 8 && nowpage < totalpage - 3 && i > nowpage + 1) {
                if (nowpage > 5 && i == nowpage + 2) {
                    append_html += '<span class="page_dot">•••</span>';
                } else if (nowpage < 7) {
                    if (i < 7) {
                        append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
                    } else if (i == 7) {
                        append_html += '<span class="page_dot">•••</span>';
                    }
                }
            }

            else {
                if (i == nowpage - 1) {
                    append_html += '<input type="button" value="' + (i + 1) + '" class="number nowpage">';
                } else {
                    append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
                }
            }
        }

        if (nowpage == totalpage) {
            append_html += '<input type="button" value="' + (i + 1) + '" class="number nowpage">';
        } else {
            append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
        }
        append_html += '<input type="button" value="下一页" class="next_page"><span>共' + totalpage + '页,到第</span><input type="text" class="targetPage"><span>页</span><input type="button" value="确定" class="ok">';
        $('.page').append(append_html);
    }
    if (totalpage == 0) {
        $('.page').html('');
    } else {
        $('.page').html('');
        showButton();
    }

    $('.page').on('click', 'input', function () {

        // 上一页
        if ($(this).hasClass('prev_page')) {
            nowpage--;
            if (nowpage < 1) {
                nowpage = 1
            }

            if (!flag) {
                $('table tbody').html('');
                showPage(nowpage);
                $('.page').html('');
                showButton();
            } else {
                $('table tbody').html('');
                searchPage(nowpage, search_value, nameOrnum);
                $('.page').html('');
                showButton();
            }
            return;
        }
        // 下一页
        else if ($(this).hasClass('next_page')) {
            nowpage++;
            if (nowpage > totalpage) {
                nowpage = totalpage;
            }

            if (!flag) {
                $('table tbody').html('');
                showPage(nowpage);
                $('.page').html('');
                showButton();
            } else {
                $('table tbody').html('');
                searchPage(nowpage, search_value, nameOrnum);
                $('.page').html('');
                showButton();
            }
            return;
        }
        // 跳转页按钮
        else if ($(this).hasClass('ok') && !$(this).hasClass('nowpage')) {
            nowpage = parseInt($('.targetPage').val());
            if ($('.targetPage').val() == null || $('.targetPage').val() == '') {
                nowpage = 1;
            }
            if (nowpage > totalpage) {
                nowpage = totalpage;
            }
            if (!flag) {
                $('table tbody').html('');
                showPage(nowpage);
                $('.page').html('');
                showButton();
            } else {
                $('table tbody').html('');
                searchPage(nowpage, search_value, nameOrnum);
                $('.page').html('');
                showButton();
            }
            return;
        }
        //点击页码事件
        else if ($(this).hasClass('number')) {
            nowpage = parseInt($(this).val());
            if (!flag) {
                $('table tbody').html('');
                showPage(nowpage);
                $('.page').html('');
                showButton();
            } else {
                $('table tbody').html('');
                searchPage(nowpage, search_value, nameOrnum);
                $('.page').html('');
                showButton();
            }
            return;
        }
    });

    // 搜索按钮
    var nameOrnum = 'name';
    $('.user_list .search select').click(function () {
        if ($(this).val() == 0) {
            $('.user_list .search .search_text').attr('placeholder', '请输入用户姓名...');
            nameOrnum = 'name';
        } else if ($(this).val() == 1) {
            $('.user_list .search .search_text').attr('placeholder', '请输入学号/工号...');
            nameOrnum = 'number';
        }
    });
    $('.user_list .search .search_btn').click(function () {
        search_value = $('.user_list .search .search_text').val();
        nowpage = 1;
        if (search_value == null || search_value == '') {
            flag = false;
            $('table tbody').html('');
            showPage(nowpage);
            totalpage = totalPage();
            $('.page').html('');
            showButton();
        } else {
            flag = true;
            $('table tbody').html('');
            searchPage(nowpage, search_value, nameOrnum);
            $.ajax({
                type: "get",
                url: "userlist?action=totalSearch&value=" + search_value + "&nameOrnum=" + nameOrnum,
                async: false,
                success: function (date) {
                    totalpage = date;
                }
            });
            if (totalpage == 0) {
                $('.page').html('');
            } else {
                $('.page').html('');
                showButton();
            }
        }
    });
});